package com.aaa.product.controller;

import com.aaa.product.entity.TbPrdInfo;
import com.aaa.product.service.TbPrdInfoService;
import com.aaa.product.service.impl.TbPrdInfoServiceImpl;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("product")
public class TbPrdInfoController {

    @Resource
    TbPrdInfoServiceImpl tbPrdInfoServiceimpl;

    @RequestMapping("selectAll")
    public List<TbPrdInfo> selectAll(){
        return tbPrdInfoServiceimpl.listDesc();
    }

    @RequestMapping("saveOrUpdate")
    public boolean saveOrUpdate(TbPrdInfo tbPrdInfo){
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(date);
        tbPrdInfo.setUpdateDate(dateString);

        return tbPrdInfoServiceimpl.saveOrUpdate(tbPrdInfo);
    }

    @RequestMapping("delete")
    public boolean delete(Integer tid){
        return tbPrdInfoServiceimpl.removeById(tid);
    }

    @RequestMapping("findById/{tid}")
    public TbPrdInfo findById(@PathVariable String tid){
        return tbPrdInfoServiceimpl.findById(tid);
    }

    //模糊查询
    @RequestMapping("listByLname")
    public List<TbPrdInfo> listByLname(String labelName){
        return tbPrdInfoServiceimpl.listByLname(labelName);
    }


    //判断要购买的数量库存是否够
    @RequestMapping("/compareStore")
    public TbPrdInfo compareStore( String tid,Integer store){
        return tbPrdInfoServiceimpl.compareStore(tid,store);
    }

    //下单后减少库存
    @RequestMapping("/subtractStore")
    public boolean subtractStore2 (String tid ,Integer store){
        return tbPrdInfoServiceimpl.subtractStore2(tid,store);
    }

    //取消支付后回滚库存
    @RequestMapping("/addStore")
    public boolean addStore (String tid ,Integer store){
        return tbPrdInfoServiceimpl.addStore(tid,store);
    }


    // 获取图片
    @RequestMapping({"/findProductPicByPath"})
    public void getFeedBackPicture(HttpServletResponse response, @RequestParam("name") String name) throws IOException {
        response.setContentType("image/jpeg;charset=utf-8");
        response.setHeader("Content-Disposition", "inline; filename=girls.png");
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(Files.readAllBytes(Paths.get(System.getProperty("user.dir")+"\\service_product\\src\\main\\resources\\img\\").resolve(name)));
        outputStream.flush();
        outputStream.close();
    }
    // 上传图片
    @RequestMapping({"/insertProductPic"})
    public Object insertProductPic(@RequestParam("file") MultipartFile avatorFile) {
        JSONObject jsonObject = new JSONObject();
        if (avatorFile.isEmpty()) {
            jsonObject.put("code", 0);
            jsonObject.put("msg", "上传失败");
            return jsonObject;
        } else {
            String fileName = System.currentTimeMillis() + avatorFile.getOriginalFilename();
            String filePath = System.getProperty("user.dir")+"\\service_product\\src\\main\\resources\\img";
            File file1 = new File(filePath);
            if (!file1.exists()) {
                file1.mkdir();
            }

            File dest = new File(filePath + System.getProperty("file.separator") + fileName);
            String storePath = fileName;

            try {
                avatorFile.transferTo(dest);
                jsonObject.put("code", 1);
                jsonObject.put("msg", "上传成功");
                jsonObject.put("pic", storePath);
                return jsonObject;
            } catch (IOException var15) {
                jsonObject.put("code", 0);
                jsonObject.put("msg", "上传失败" + var15.getMessage());
                return jsonObject;
            }
        }
    }
    // 修改图片
    @RequestMapping({"/updateProductPic"})
    public Object updateProductPic(@RequestParam("file") MultipartFile avatorFile, @RequestParam("tid") String tid) {
        JSONObject jsonObject = new JSONObject();
        if (avatorFile.isEmpty()) {
            jsonObject.put("code", 0);
            jsonObject.put("msg", "上传失败");
            return jsonObject;
        } else {
            String fileName = System.currentTimeMillis() + avatorFile.getOriginalFilename();
            String filePath = System.getProperty("user.dir")+"\\service_product\\src\\main\\resources\\img";
            File file1 = new File(filePath);
            if (!file1.exists()) {
                file1.mkdir();
            }

            File dest = new File(filePath + System.getProperty("file.separator") + fileName);
            String storePath = fileName;

            try {
                avatorFile.transferTo(dest);
                TbPrdInfo tbPrdInfo = new TbPrdInfo();
                tbPrdInfo.setTid(tid);
                tbPrdInfo.setPicPath(storePath);
                boolean flag = tbPrdInfoServiceimpl.updateById(tbPrdInfo);
                if (flag) {
                    jsonObject.put("code", 1);
                    jsonObject.put("msg", "上传成功");
                    jsonObject.put("pic", storePath);
                    return jsonObject;
                } else {
                    jsonObject.put("code", 0);
                    jsonObject.put("msg", "上传失败");
                    return jsonObject;
                }
            } catch (IOException var15) {
                jsonObject.put("code", 0);
                jsonObject.put("msg", "上传失败" + var15.getMessage());
                return jsonObject;
            }
        }
    }


}
